import axios from 'axios';
import {message} from 'ant-design-vue';

import {
    getLocalStorage,
    clearLocalStorage
} from "@/utils/localstore";

const instance = axios.create({
    timeout: 8000000
});

instance.interceptors.request.use(
    (config) => {
        if (getLocalStorage('token')) {
            config.headers['Token'] = getLocalStorage('token')
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
);

instance.interceptors.response.use(
    async (response) => {
        const {data} = response
        if (data.code === 10003) {
            message.error('登陆超时')
            await clearLocalStorage();
            setTimeout(()=>{
                window.location.reload();
            }, 1500)
            return response.data;
        }
        return response.data;
    },

    (error) => {
        return Promise.reject(error)
    }
);

export default instance
